package com.haowu.customer.dubbo;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.haowu.common.core.constant.UserConstants;
import com.haowu.common.core.enums.AuthType;
import com.haowu.common.core.enums.UserStatus;
import com.haowu.common.core.enums.UserType;
import com.haowu.common.core.exception.user.UserException;
import com.haowu.common.core.utils.BeanCopyUtils;
import com.haowu.customer.api.RemoteCustomerService;
import com.haowu.customer.api.domain.HwAppAccount;
import com.haowu.customer.api.domain.HwAppSocialAuth;
import com.haowu.customer.api.model.CustomerInfo;
import com.haowu.customer.api.model.LoginCustomer;
import com.haowu.customer.api.model.XcxLoginCustomer;
import com.haowu.customer.mapper.HwAppAccountMapper;
import com.haowu.customer.mapper.HwAppCustomerMapper;
import com.haowu.customer.mapper.HwAppSocialAuthMapper;
import com.haowu.customer.service.IHwAppAccountService;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

/**
 * app会员登录
 *
 * @author Lion Li
 */
@RequiredArgsConstructor
@Service
@DubboService
public class RemoteCustomerLoginServiceImpl implements RemoteCustomerService {
    private final HwAppCustomerMapper customerMapper;
    private final IHwAppAccountService accountService;
    private final HwAppAccountMapper accountMapper;
    private final HwAppSocialAuthMapper authMapper;

    @Override
    public LoginCustomer getCustomerInfo(String account) throws UserException {
        HwAppAccount cusInfo = accountMapper.selectOne(new LambdaQueryWrapper<HwAppAccount>()
            .select(HwAppAccount::getUserAccount, HwAppAccount::getStatus)
            .eq(HwAppAccount::getUserAccount, account));
        if (ObjectUtil.isNull(cusInfo)) {
            throw new UserException("user.not.exists", account);
        }
        if (UserStatus.DISABLE.getCode().equals(cusInfo.getStatus())) {
            throw new UserException("user.blocked", account);
        }
        // 此处可根据登录用户的数据不同 自行创建 loginUser
        return buildLoginUser(customerMapper.selectUserByAccount(account));
    }

    @Override
    public LoginCustomer getCustomerInfoByPhonenumber(String phonenumber) throws UserException {
        HwAppAccount cusInfo = accountMapper.selectOne(new LambdaQueryWrapper<HwAppAccount>()
            .select(HwAppAccount::getUserPhone, HwAppAccount::getStatus)
            .eq(HwAppAccount::getUserPhone, phonenumber));
        if (ObjectUtil.isNull(cusInfo)) {
            throw new UserException("user.not.exists", phonenumber);
        }
        if (UserStatus.DISABLE.getCode().equals(cusInfo.getStatus())) {
            throw new UserException("user.blocked", phonenumber);
        }
        // 此处可根据登录用户的数据不同 自行创建 loginUser
        return buildLoginUser(customerMapper.selectUserByPhonenumber(phonenumber));
    }

    @Override
    public XcxLoginCustomer getCustomerInfoByOpenid(String openid) throws UserException {
        //社交账号授权表查询是否存在openid匹配的小程序账号记录
        HwAppSocialAuth socialAuth = authMapper.selectOne(new LambdaQueryWrapper<HwAppSocialAuth>()
            .select(HwAppSocialAuth::getUid, HwAppSocialAuth::getIsDelete)
            .eq(HwAppSocialAuth::getAuthIdentification, openid)
            .eq(HwAppSocialAuth::getAuthType, AuthType.WX_XCX.getAuthType()));
        if (ObjectUtil.isNull(socialAuth)) {
            throw new UserException("user.not.exists");
        }
        //如果存在授权记录，根据uid查询该用户的账号信息
        HwAppAccount cusInfo = accountMapper.selectOne(new LambdaQueryWrapper<HwAppAccount>()
            .select(HwAppAccount::getUserPhone, HwAppAccount::getStatus)
            .eq(HwAppAccount::getUid, socialAuth.getUid()));
        if (ObjectUtil.isNull(cusInfo)) {
            throw new UserException("user.not.exists");
        }
        if (UserStatus.DISABLE.getCode().equals(cusInfo.getStatus())) {
            throw new UserException("user.blocked");
        }
        //根据账号信息中的手机号反查出用户信息并构造xcxloginUser
        CustomerInfo loginUser = customerMapper.selectUserByPhonenumber(cusInfo.getUserPhone());
        XcxLoginCustomer xcxLoginCustomer = (XcxLoginCustomer) buildLoginUser(loginUser);
        xcxLoginCustomer.setOpenId(openid);
        return xcxLoginCustomer;
    }

    @Override
    public Boolean registerCustomer(HwAppAccount appAccount) {
        String account = appAccount.getUserAccount();
        if (UserConstants.NOT_UNIQUE.equals(accountService.checkAccountUnique(appAccount))) {
            throw new UserException("user.register.save.error", account);
        }
        return accountService.registerUser(appAccount);
    }

    /**
     * 构建登录用户
     */
    private LoginCustomer buildLoginUser(CustomerInfo customerInfo) {
        LoginCustomer loginCustomer = BeanCopyUtils.copy(customerInfo, LoginCustomer.class);
        loginCustomer.setUserId(customerInfo.getId());
        loginCustomer.setUsername(customerInfo.getUserName());
        loginCustomer.setUserType(UserType.CUS_USER.getUserType());
        return loginCustomer;
    }

}
